/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.fca.daos;

import db.dao.AbstractDAO;
import db.orm.BeansGenerator;
import edu.fca.beans.ProfesorBean;
import java.util.ArrayList;

/**
 *
 * @author Romms
 */
public class ProfesorDAO extends AbstractDAO<ProfesorBean>{

    @Override
    public void add(ProfesorBean entity) throws Exception {
        super.setQuery("INSERT INTO profesor (" +
            "codigo," +
            "apellidoP," +
            "apellidoM,"+
            "nombre) " +
            "VALUES" + "('"+ entity.getCodigo()+ "','" +
            entity.getApellidoP()+ "','" +
            entity.getApellidoM()+ "','" +
            entity.getNombre()+ "')");
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    public void deleteWhere(String condition) throws Exception {
        super.setQuery("DELETE FROM profesor WHERE " + condition);
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    public void updateWhere(ProfesorBean entity, String condition) throws Exception {
        super.setQuery("UPDATE profesor SET " +
            "codigo='"+entity.getCodigo()+
             "',apellidoP='"+ entity.getApellidoP()+
             "',apellidoM='"+ entity.getApellidoM()+
             "',nombre='"+ entity.getNombre()+
             "' WHERE "+ condition);
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    protected ArrayList<ProfesorBean> select() throws Exception {
        ArrayList<ProfesorBean> list  =BeansGenerator.getInstance().generateListOfBeans
                (super.connectionManager.getConnection().executeQuery(this.getQuery()), ProfesorBean.class);
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
        return list;
    }

    @Override
    public ArrayList<ProfesorBean> selectAll() throws Exception {
        this.setQuery("SELECT * FROM profesor");
        return this.select();
    }

    @Override
    public ArrayList<ProfesorBean> selectWhere(String condition) throws Exception {
        this.setQuery("SELECT * FROM profesor WHERE "+condition);
        return this.select();
    }

}
